package com.halflife.framework.participant.range.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.halflife.framework.participant.range.RangeInfo;
import com.halflife.framework.participant.range.RoleMembership;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RangeUtil {

    public static String[] getMenuRangeTree(List<RangeInfo> rangeInfoList){
        StringBuilder sb = new StringBuilder();
        Map<Long, Menu> fmap = new HashMap<Long, Menu>();
        List<Menu> menuList = new ArrayList<Menu>();
        for(RangeInfo range:rangeInfoList){
            //菜单
            if(Integer.valueOf(range.getRangeType())== 0 ){
                RangeInfo frange = range.getRangeInfo();

                Menu menu;
                if(frange==null){
                    menu = new Menu(range);
                    fmap.put(Long.valueOf(range.getId()), menu);
                    List list = new ArrayList();
                    menu.setMenus(list);
                    menuList.add(menu);
                    continue;
                }
                Menu zm = new Menu(range);
                if(!fmap.containsKey(frange.getId())){
                    menu = new Menu(frange);
                    fmap.put(Long.valueOf(frange.getId()), menu);
                    List list = new ArrayList();
                    list.add(zm);
                    menu.setMenus(list);
                    menuList.add(menu);
                }else{
                    menu = fmap.get(frange.getId());
                    menu.getMenus().add(zm);
                }
            }else{
                if(sb.length()>0){
                    sb.append(";");
                }
                sb.append(range.getRangeNo());
            }

        }
        Gson gson = new GsonBuilder().excludeFieldsWithModifiers().create();
        String[] s = {gson.toJson(menuList),sb.toString()};
        return s;
    }

    /**
     * 获取角色Map
     * @param userRoles
     * @param roleIds
     * @return
     */
    public static Map<String, String> getRoleMap(List<RoleMembership> userRoles,List<String> roleIds){

        Map<String, String> roleMap = new HashMap<String, String>();
        for(RoleMembership role:userRoles){
            //推送角色Ids
            roleIds.add(String.valueOf(role.getRoleId()));
            //推送角色Map
            roleMap.put(role.getRole().getRoleNo(),role.getRole().getRoleName());
        }
        return roleMap;
    }

    /**
     * 获取权限菜单
     * @param rangeInfoList
     * @return
     */
    public static List<Menu> getMenuRangeList(List<RangeInfo> rangeInfoList){

        Map<Integer, Menu> fmap = new HashMap<Integer, Menu>();
        List<Menu> menuList = new ArrayList<Menu>();
        for(RangeInfo range:rangeInfoList){
            //菜单
            if(Integer.valueOf(range.getRangeType())== 0 ){
                RangeInfo frange = range.getRangeInfo();

                Menu menu;
                if(frange==null){
                    menu = new Menu(range);
                    fmap.put(Integer.valueOf(range.getId()), menu);
                    List list = new ArrayList();
                    menu.setMenus(list);
                    menuList.add(menu);
                    continue;
                }
                frange.setId(range.getRangeInfoId());
                Menu zm = new Menu(range);
                if(!fmap.containsKey(frange.getId())){
                    menu = new Menu(frange);
                    fmap.put(frange.getId(), menu);
                    List list = new ArrayList();
                    list.add(zm);
                    menu.setMenus(list);
                    menuList.add(menu);
                }else{
                    menu = fmap.get(frange.getId());
                    menu.getMenus().add(zm);
                }
            }else
                continue;
        }
        return menuList;
    }
}
